package com.vehicle.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vehicle.bean.DataGridView;
import com.vehicle.bean.PageVo;
import com.vehicle.bean.ResultDto;
import com.vehicle.mapper.BtypeMapper;
import com.vehicle.mapper.CarMapper;
import com.vehicle.mapper.PetrolMapper;
import com.vehicle.mapper.PetrolRecordMapper;
import com.vehicle.pojo.Btype;
import com.vehicle.pojo.Car;
import com.vehicle.pojo.Petrol;
import com.vehicle.pojo.PetrolRecord;
import com.vehicle.service.PetrolRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PetrolRecordServiceImpl implements PetrolRecordService {

    @Autowired
    private PetrolRecordMapper petrolRecordMapper;

    @Autowired
    private CarMapper carMapper;

    @Autowired
    private BtypeMapper btypeMapper;

    @Autowired
    private PetrolMapper petrolMapper;

    @Override
    public List<Car> findAllCar() {
        return carMapper.selectAllCar();
    }

    @Override
    public DataGridView findAllPetrolRecord(Integer carid, PageVo pageVo) {
        PageHelper.startPage(pageVo.getPage(),pageVo.getLimit());
        List<PetrolRecord> allPetrolRecord = petrolRecordMapper.findAllPetrolRecord(carid);
        PageInfo pageInfo = new PageInfo(allPetrolRecord);
        DataGridView dataGridView = new DataGridView();
        dataGridView.setCount(pageInfo.getTotal());
        dataGridView.setData(allPetrolRecord);
        return dataGridView;
    }

    @Override
    public ResultDto deleteIds(String idArray) {
        ResultDto resultDto = new ResultDto();
        try{
            petrolRecordMapper.deleteIds(idArray);
            resultDto.setCode(1001);
            resultDto.setMsg("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            resultDto.setCode(-1001);
            resultDto.setMsg("删除失败");
        }
        return resultDto;
    }

    @Override
    public List<Btype> findAllBtypeByPetrol() {
        return btypeMapper.findAllBtypeByPetrol();
    }

    @Override
    public PetrolRecord findFinalRecord(Integer carid) {
        return petrolRecordMapper.findFinalRecord(carid);
    }

    @Override
    public List<Petrol> findAllPetrol() {
        return petrolMapper.findAllPetrol();
    }

    @Override
    public ResultDto petrolRecordAdd(PetrolRecord petrolRecord) {
        ResultDto resultDto = new ResultDto();
        try {
            petrolRecordMapper.petrolRecordAdd(petrolRecord);
            resultDto.setCode(1001);
            resultDto.setMsg("添加成功");
        }catch (Exception e){
            e.printStackTrace();
            resultDto.setCode(-1001);
            resultDto.setMsg("添加失败");
        }
        return resultDto;
    }

    @Override
    public PetrolRecord findPetrolRecordById(Integer petrolid) {
        PetrolRecord petrolRecord = petrolRecordMapper.findPetrolRecordById(petrolid);
        return petrolRecord;
    }

    @Override
    public ResultDto updatePetrolRecord(PetrolRecord petrolRecord) {
        ResultDto resultDto = new ResultDto();
        try {
            petrolRecordMapper.petrolRecordUpdate(petrolRecord);
            resultDto.setCode(1001);
        }catch (Exception e){
            e.printStackTrace();
            resultDto.setCode(-1001);
        }
        return resultDto;
    }

    @Override
    public ResultDto deletePetrolRecord(Integer petrolid) {
        ResultDto resultDto = new ResultDto();
        try {
            petrolRecordMapper.petrolRecordDelete(petrolid);
            resultDto.setCode(1001);
            resultDto.setMsg("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            resultDto.setCode(-1001);
            resultDto.setMsg("删除失败");
        }
        return resultDto;
    }

}
